#include "StandSniper.h"
#include "ShareComponents.h"
#include "RSManager.h"
#include "Box.h"
#include "Sprite.h"
#include "BulletManager.h"
#define PI 3.14

#define GUN_ANGLE 0.39225 //PI/8
#define ANGLE 1.0467 // PI/3
StandSniper::StandSniper(Box* box):Object(box,ObjStandSniper)
{
}


StandSniper::~StandSniper(void)
{
}



void StandSniper::init()
{
	_left_gun = RSManager::getInstance()->getSprite(SOLDIER_LEFT_GUN);
	_right_gun = RSManager::getInstance()->getSprite(SOLDIER_RIGHT_GUN);
	_left_top_gun = RSManager::getInstance()->getSprite(SOLDIER_LEFT_TOP_GUN);
	_righ_top_gun = RSManager::getInstance()->getSprite(SOLDIER_RIGH_TOP_GUN);
	_left_bottom_gun = RSManager::getInstance()->getSprite(SOLDIER_LEFT_BOTTOM_GUN);
	_right_bottom_gun = RSManager::getInstance()->getSprite(SOLDIER_RIGHT_BOTTOM_GUN);
	delay = 0;
	delay2 = 0;
	player = gl_player;
	_sprite = _left_gun;	
}
void StandSniper::update()
{
	delay+=GAME_TIME;
	delay2+=GAME_TIME;

	if (delay > 900)
	{
		delay = 0;
		_angle = GetAngle();
		int index = GetIndex();
		int gunindex = SetGunIndex();
		if (abs(_box->_position.x - gl_player->_position.x) < 400)
		{
			_sprite->Next();
			BulletManager::getInstance()->shoot(B_E,this->_obj,_box->_position + SetPosGun(index),gunindex*GUN_ANGLE);		
		}
	}	
}
int StandSniper::GetIndex()
{
	// moi goc hien sprite 45 do
	return (int)(_angle /ANGLE+0.5f)%6;// 2.4->2, 2.7->3
}
int StandSniper::SetGunIndex()
{
	return (int)(_angle / GUN_ANGLE+0.5f)%16;

}
float StandSniper::GetAngle()
{
	float doi = gl_player->getCenterPos().y - _box->getCenterPos().y;
	float ke = gl_player->getCenterPos().x - _box->getCenterPos().x;
	return atan2(doi, ke)>0 ? atan2(doi, ke) : 2 * PI + atan2(doi, ke);
}
D3DXVECTOR2 StandSniper::SetPosGun(int index)
{ 
	switch (index)
	{
	case 0: _sprite = _right_gun; return D3DXVECTOR2(48, 50); 
	case 1:	_sprite = _righ_top_gun; return D3DXVECTOR2(40, 78); 
	case 2:	_sprite = _left_top_gun; return D3DXVECTOR2(0, 78); 
	case 3:_sprite = _left_gun; return D3DXVECTOR2(0, 50);
	case 4: _sprite = _left_bottom_gun; return D3DXVECTOR2(0, 26);
	case 5: _sprite = _right_bottom_gun; return D3DXVECTOR2(48, 26); 
	default: return	D3DXVECTOR2(0, 0);
	}	
}
void StandSniper::render()
{
	_sprite->Render(_box->_position);		
}
